#include "stdafx.h"



#include "qgraphics.h"

using namespace micro::gui;

using namespace micro::utils;




void QGraphics::Rectangle( const QRect & r )
{
    ::Rectangle( hdc, r.left, r.top, r.right, r.bottom );
}
void QGraphics::RoundRect( const QRect & r, int w, int h )
{
    ::RoundRect( hdc, r.left, r.top, r.right, r.bottom, w, h );
}
int  QGraphics::DrawText( const QString & str,  QRect & rect, UINT mode )
{
    return ::DrawText( hdc, str, str.GetLength(), rect, mode );
}
int  QGraphics::DrawText( LPCTSTR str,  QRect & rect, UINT mode )
{
    if ( str )
        return ::DrawText( hdc, str, ( int )_tcslen( str ), rect, mode );

    return 0;
}
void QGraphics::DrawEdge( QRect & r, UINT nEdge, UINT flags )
{
    ::DrawEdge( hdc, r, nEdge, flags );
}

void QGraphics::MoveTo( int cx, int cy )
{
    ::MoveToEx( hdc, cx, cy, nullptr );
}
void QGraphics::LineTo( int cx, int cy )
{
    ::LineTo( hdc, cx, cy );

}

void QGraphics::Draw3dRect( QRect  & rc, QCOLOR colorTopLeft, QCOLOR colorBottomRight )
{
    int x  = rc.left;
    int y  = rc.top;
    int cx = rc.Width();
    int cy = rc.Height();

    FillSolidRect( QRect( x, y, x + cx - 1, y + 1 ), colorTopLeft );
    FillSolidRect( QRect( x, y, x + 1, y + cy - 1 ), colorTopLeft );
    FillSolidRect( QRect( x + cx, y, x + cx + 1, y + cy ), colorBottomRight );
    FillSolidRect( QRect( x, y + cy, x + cx, y + cy - 1 ), colorBottomRight );
}

void QGraphics::FillSolidRect( const QRect  & rc,  QCOLOR clr )
{
    QCOLOR c = ::SetBkColor( hdc, clr );
    ::ExtTextOut( hdc, 0, 0, ETO_OPAQUE, &rc, nullptr, 0, nullptr );
    ::SetBkColor( hdc, c );
}

void QGraphics::ExtTextOut( LPCTSTR str, const QRect  & rc,  QCOLOR clr, UINT fuOptions, int x , int y )
{
    ::ExtTextOut( hdc, x, y, fuOptions, &rc, str, _tcslen( str ), nullptr );
}

void QGraphics::ExtTextOut( LPCTSTR str, int count, const QRect  & rc,  QCOLOR clr, UINT fuOptions, int x , int y )
{
    ::ExtTextOut( hdc, x, y, fuOptions, &rc, str, count, nullptr );
}

void QGraphics::FillRect( QRect & lprc, QBrush * pBruhs )
{
    //if (IsValidHandle())
    ::FillRect( hdc, lprc, ( HBRUSH )pBruhs->GetBrush() );
}

void QGraphics::FillRect( QRect & rc, HBRUSH hBrush )
{
    ::FillRect( hdc, rc, hBrush );
}

void QGraphics::FrameRect( QRect & rc, HBRUSH hBrush )
{
    ::FrameRect( hdc, rc, hBrush );
}



//#if (!defined(_WIN32_WCE) || (_WIN32_WCE >= 420))
//bool QGraphics::GradientFill(const PTRIVERTEX pVertices, DWORD nVertices, void* pMeshElements, DWORD nMeshElements, DWORD dwMode)
//{
//	ASSERT(hdc != nullptr);
//	return (bool) ::GradientFill(hdc, pVertices, nVertices, pMeshElements, nMeshElements, dwMode);
//}
//#endif // !defined(_WIN32_WCE) || (_WIN32_WCE >= 420)




//////////////////////////////////////////////////////////////////////////////////////////////////////////


QPaintGraphics::QPaintGraphics( QWindow * p )
{
    hwnd = p->GetHandle();

    hdc = ::BeginPaint( hwnd, &ps );
}

QPaintGraphics::~QPaintGraphics( void )
{

    ::EndPaint( hwnd,  &ps );
    Detach();
}

QRect  QPaintGraphics::GetPaintRect()
{
    QRect rcPaint = ps.rcPaint;
    return rcPaint;
}

////////////////////////////////////////////////////////////////
QClientGraphics::QClientGraphics( QWindow * p )
{
    hwnd = nullptr;

    if ( p )
        CreateGraphics( p );
}

void QClientGraphics::CreateGraphics( QWindow * p )
{
    //m_bDestroy = true;
    hwnd = p->GetHandle();
    hdc = ::GetDC( hwnd ); //ReleaseDC
}

QClientGraphics::~QClientGraphics( void )
{
    if ( hwnd )
        ::ReleaseDC( hwnd, Detach() );

}



QWindowGraphics::QWindowGraphics( QWindow * p )
{
    ASSERT( p );
    hwnd =   p->GetHandle();
    hdc = ::GetWindowDC( hwnd );

////   HDC hdc;
////   hdc = GetDCEx(hwnd, (HRGN)wParam, DCX_WINDOW|DCX_INTERSECTRGN);
////   // Paint into this DC
////   ReleaseDC(hwnd, hdc);
}



QWindowGraphics::~QWindowGraphics()
{
    //ASSERT(hdc != nullptr);
    ::ReleaseDC( hwnd, Detach() );
}

